package com.wngbms.aop.dynamicproxy;

import com.wngbms.aop.staticproxy.EchoService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class EchoServiceInvocation implements InvocationHandler {

    private final EchoService echoService;

    public EchoServiceInvocation(EchoService echoService) {
        this.echoService = echoService;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (EchoService.class.isAssignableFrom(method.getDeclaringClass())) {
            System.out.println("pre");
            String echo = echoService.echo((String) args[0]);
            System.out.println("end");
            return echo;
        }
        return null;
    }
}
